//
//  LoginViewController.m
//  JZMCFM
//
//  Created by Mc on 15/11/9.
//  Copyright (c) 2015年 陈卓. All rights reserved.
//

#import "LoginViewController.h"
#import <BmobSDK/Bmob.h>
#import "NSString+Hash.h"
#import "MyViewController.h"
#import "RegisterViewController.h"
@interface LoginViewController ()
@property (strong, nonatomic) IBOutlet UITextField *usernameL;
@property (strong, nonatomic) IBOutlet UITextField *passwordL;
@property (strong, nonatomic) IBOutlet UIButton *loginButton;
@property (strong, nonatomic) IBOutlet UIButton *regisButton;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 加边框
    self.loginButton.layer.borderWidth = 1;
    self.loginButton.layer.cornerRadius = 10;
    self.regisButton.layer.borderWidth = 1;
    self.regisButton.layer.cornerRadius = 10;
    
    // 加边框颜色
    self.loginButton.layer.borderColor = [UIColor orangeColor].CGColor;
    self.regisButton.layer.borderColor = [UIColor orangeColor].CGColor;
    
}



- (IBAction)loginButton:(id)sender {
    BmobQuery *query = [BmobQuery queryWithClassName:@"_User"];
    [query whereKey:@"username" equalTo:self.usernameL.text];
    [query whereKey:@"password" equalTo:self.passwordL.text];
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        
        if ([self.usernameL.text isEqualToString:@""] || [self.passwordL.text isEqualToString:@""]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入用户名或密码" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }else{
            if (array.count) {
                [[LoginMangager sharedInstance] loginSuccessHandle];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"登陆成功" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alert show];
                MyViewController *vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"aa"];
                [self.navigationController pushViewController:vc animated:YES];
            }else{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入的密码有误" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [alert show];
            }
        }
        
    }];
}
- (IBAction)registerButton:(id)sender {
    RegisterViewController *vc = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
